#include "cache.h"
#include "log.h"
#include "def.h"
#include "util.h"


Cache::Cache()
{
}
Cache::~Cache()
{
}

bool Cache::Set(const std::string& uuid, const std::uint32_t uid, const std::string& agency, const std::string& username, const std::uint32_t agency_id, const std::string& token, const std::string& hall_url, const std::string& platform, const std::int64_t balance, const std::string& currency, const std::string& ext)
{
    if (nullptr == m_userinfoRedis)
    {
        log_error("m_userinfoRedis is null");
        return false;
    }
    std::string id_key = USER_ID_KEY + uuid;
    if (0 != m_userinfoRedis->Set(id_key, std::to_string(uid)))
    {
        log_error("Hset %s data failed", id_key.c_str());
        return false;
    }
    m_userinfoRedis->Expire(id_key, g_cache_dur);

    std::string key = USER_KEY + std::to_string(uid);
    std::vector<std::string> field = {"uuid", "agency", "agency_id", "username", "token", "hall_url", "platform" ,"balance", "currency", "ext" };
    std::vector<std::string> vals = {uuid, agency, std::to_string(agency_id), username, token, hall_url, platform, std::to_string(balance), currency, ext };
    
    if (0 != m_userinfoRedis->Hmset(key, field, vals))
    {
        log_error("Hmset %s data failed", key.c_str());
        return false;
    }
    
    m_userinfoRedis->Expire(key, g_cache_dur);
    return true;
}


bool Cache::InfoSet(const std::string& uuid, const std::uint32_t uid, const std::string& agency, const std::string& username, const std::uint32_t agency_id, const std::string& token, const std::string& hall_url, const std::string& platform, const std::int64_t balance, const std::string& currency, const std::string& ext)
{
	if (nullptr == m_userinfoRedis)
	{
		log_error("m_userinfoRedis is null");
		return false;
	}

	std::string key = USER_KEY + std::to_string(uid);
	static std::vector<std::string> field = { "uuid", "agency", "agency_id", "username", "token", "hall_url", "platform" ,"balance", "currency", "ext"  };
	std::vector<std::string> vals = { uuid, agency, std::to_string(agency_id), username, token, hall_url, platform, std::to_string(balance), currency, ext };

	if (0 != m_userinfoRedis->Hmset(key, field, vals))
	{
		log_error("Hmset %s data failed", key.c_str());
		return false;
	}

	m_userinfoRedis->Expire(key, g_cache_dur);
	return true;
}

bool Cache::InfoGet(const std::uint32_t uid, std::string& uuid, std::string& agency, std::string& username, std::uint32_t& agency_id, std::string& token, std::string& hall_url, std::string& platform, std::int64_t& balance, std::string& currency, std::string& ext)
{
    if (nullptr == m_userinfoRedis)
    {
        log_error("m_userinfoRedis is null");
        return false;
    }
    std::string key = USER_KEY + std::to_string(uid);
    static std::vector<std::string> field = { "uuid", "agency", "agency_id", "username", "token", "hall_url", "platform" ,"balance", "currency", "ext", "currency_unit_multi", "currency_label" };
    std::map<std::string, std::string> params;
    if (0 != m_userinfoRedis->Hmget(key, field, &params))
    {
        log_error("Hmget %s data failed", key.c_str());
        return false;
    }
    // if (params.size() != field.size())
    // {
    //     log_error("Hmget %s params size not match:%s", key.c_str(), CTools::ToMapString(params).c_str());
    //     return false;
    // }
    uuid = params["uuid"];
    agency = params["agency"];
    username = params["username"];
    agency_id = ::atoll(params["agency_id"].c_str());
    token = params["token"];
    hall_url = params["hall_url"];
    platform = params["platform"];
    balance = atoll(params["balance"].c_str());
    currency = params["currency"];
    ext = params["ext"];

    return true;
}

bool Cache::TokenUpdate(const std::uint32_t uid, const std::string& token)
{
	if (nullptr == m_userinfoRedis)
	{
		log_error("m_userinfoRedis is null");
		return false;
	}

	std::string key = USER_KEY + std::to_string(uid);

    std::map<std::string, std::string> fv;
    fv["token"] = token;
	if (0 != m_userinfoRedis->Hmset(key, fv))
	{
		log_error("Hmset %s data failed", key.c_str());
		return false;
	}

	m_userinfoRedis->Expire(key, g_cache_dur);
	return true;
}

bool Cache::Uid(const std::string& uuid, std::uint32_t& uid)
{
    if (nullptr == m_userinfoRedis)
    {
        log_error("m_userinfoRedis is null");
        return false;
    }

    std::string id_key = USER_ID_KEY + uuid;
    std::string val = "";
    if (0 != m_userinfoRedis->Get(id_key, &val))
    {
        log_error("get %s data failed", id_key.c_str());
        return false;
    }

    if (val.empty())
        uid = 0;
    else
        uid = ::strtoul(val.c_str(), NULL, 10);

    return true;
}

bool Cache::UidSet(const std::string& uuid, const std::uint32_t uid)
{
	if (nullptr == m_userinfoRedis)
	{
		log_error("m_userinfoRedis is null");
		return false;
	}

	std::string id_key = USER_ID_KEY + uuid;
	std::string val = std::to_string(uid);
	if (0 != m_userinfoRedis->Set(id_key, val))
	{
		log_error("get %s data failed", id_key.c_str());
		return false;
	}
    m_userinfoRedis->Expire(id_key, g_cache_dur);
	return true;
}







